<template>
	<view>
		<image class="mytopbg" :style="{ height: (stausBarHeight*1+80) + 'px' }" src="@/static/headtopbg.png"
			mode="aspectFill" />
		<page-head :title="'产品分类'" :bgColor="`transparent`" :headtype="1"></page-head>
		<view class="topblock" :style="{ paddingTop: (stausBarHeight*1) + 'px'}">
			<view class="search flex marginAuto">
				<view class="searchicon">
					<u-icon name="search" color="#999999" size="50"></u-icon>
				</view>
				<view class="searchinput">
					<input type="text" v-model="searchtxt" placeholder="请输入关键字">
				</view>
				<view class="searchbtn mainBg" @click="searchfun()">搜索</view>
			</view>
		</view>
		<view class="classblock flex" style="height:74vh;" :style="{ paddingTop: (stausBarHeight*1) + 'px'}">
			<view class="classl">
				<view class="classleach ellipsis01 marginAuto" :class="cateidac == item.id ? 'mainColor':''"
					@click="classfun(item,index)" v-for="(item,index) in classdata" :key='index'>{{ item.title}}</view>
			</view>
			<view class="classr">
				<view class="classrmain flex">
					<view class="classreach" v-for="(item,index) in goodsdata" :key="index">
						<view class="classmallz" v-if="item.discount">{{ item.discount.title }}</view>
						<image class="classreachbg" style="width: 260rpx;height:370rpx;" src="@/static/classifybg.png"
							mode="aspectFill"></image>
						<view class="collect" @click="collectfun(item,index)">
							<u-icon v-if="item.collection != 0" name="star-fill" color="#D31E11" size="50"></u-icon>
							<u-icon v-if="item.collection == 0" name="star" color="#333333" size="50"></u-icon>
						</view>
						<view class="classreachmsg" @click="navigate('/pages/homeList/detailedness/detailedness','type=1&id='+item.id)">
							<view class="classrgoodsimg marginAuto">
								<image style="width:216rpx;height:216rpx;border-radius:20rpx;"
									:src="URLimg+item.image" mode="aspectFill"></image>
							</view>
							<view class="classrgoodsname ellipsis01" v-text="item.name"></view>
							<view class="classrgoodsmoney"><text
									style="font-size:24rpx;">￥</text>{{ item.sell_price }}</view>
						</view>
					</view>
					<view class="classreach" v-if="goodsdata.length%2 != 0"></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				apifun:this.apifun,
				URLimg:this.URLimg,
				stausBarHeight: uni.getStorageSync('ClientRect').top + uni.getStorageSync('ClientRect').height + 10,
				page: 1,
				searchtxt: '',
				classdata: [],
				goodsdata: [],
				cateidac: 0
			};
		},
		onLoad() {
			this.classifyData()
		},
		methods: {
			classfun(item, index) { // 点击分类
				this.classifyac = index;
				this.page = 1;
				this.goodsdata = [];
				this.cateidac = item.id;
				this.goodsgetfun();
			},
			searchfun() { // 搜索
				this.page = 1;
				this.goodsdata = [];
				this.cateidac = '';
				this.goodsgetfun();
			},
			collectfun(item, index) { // 收藏商品
				let sendData = {
					goods_id: item.id
				};
				this.apifun.unirequest('/api/goods/collect', 'post', sendData, (res) => {
					// console.log(res)
					if (res.code === 200) {
						let datas = res.data;
						let ycollection = this.goodsdata[index].collection;
						this.goodsdata[index].collection = ycollection == 0 ? 1 : 0;
					} else {
						this.apifun.toast(res.msg)
					}
				})
			},
			classifyData() { // 获取分类
				let sendData = {};
				this.apifun.unirequest('/api/index/categoryList', 'post', sendData, (res) => {
					// console.log(res)
					if (res.code === 200) {
						let datas = res.data;
						this.classdata = datas;
						this.cateidac = datas[0].id;
						this.goodsgetfun()
					} else {
						this.apifun.toast(res.msg)
					}
				},true)
			},
			goodsgetfun() { // 获取商品
				let sendData = {
					page: this.page,
					limit: '200',
					keyword: this.searchtxt,
					category_id: this.cateidac,
					parameter: ''
				};
				this.apifun.unirequest('/api/goods/getGoodsLists', 'post', sendData, (res) => {
					// console.log(res)
					if (res.code === 200) {
						let datas = res.data;
						if (datas.data.length <= 0) {
							this.page--
							return false
						}
						this.goodsdata.push(...datas.data)
					} else {
						this.apifun.toast(res.msg)
					}
				},true)
			},
			navigate(urls,params){
				if (!uni.getStorageSync('token')) {
					this.apifun.toast('请登录后查看')
					return false
				};
				this.apifun.navigate(urls,params)
			}
		},
		// onReachBottom() {
		// 	this.page++;
		// 	this.initData();
		// },
		onPullDownRefresh() {
			this.page = 1;
			// this.evdata = [];
			// this.initData();
			uni.stopPullDownRefresh();
		},
	};
</script>

<style scoped lang="scss">
	.mytopbg,
	.topblock {
		position: fixed;
		top: 0;
		left: 0;
		width: 100vw;
		z-index: 99;
	}

	.search {
		width: 688rpx;
		height: 60rpx;
		line-height: 60rpx;
		padding: 5rpx 0;
		background-color: #fff;
		border-radius: 100rpx;
		justify-content: space-between;
		margin-top: 50rpx;

		.searchicon {
			width: 50rpx;
			height: 60rpx;
			padding: 0 20rpx;
			display: flex;
			border-right: 1px solid #E5E5E5;
		}

		.searchinput {
			width: 60%;
			height: 60rpx;

			input {
				width: 90%;
				padding: 0 5%;
				height: 60rpx;
				line-height: 60rpx;
				font-size: 30rpx;
				color: #333;
			}
		}

		.searchbtn {
			width: 158rpx;
			height: 60rpx;
			text-align: center;
			font-size: 30rpx;
			color: #fff;
			border-radius: 100rpx;
		}
	}

	// 产品
	.classblock {
		position: fixed;
		top: 170rpx;
		width: 100%;
	}

	.classl {
		position: absolute;
		width: 166rpx;
		height: 87%;
		overflow-y: scroll;
		border-right: 1px solid #CDCDCD;
		padding-bottom: 100rpx;

		.classleach {
			width: 95%;
			text-align: center;
			color: #999999;
			font-size: 30rpx;
			line-height: 50rpx;
			padding: 26rpx 0 30rpx;
		}
	}

	.classr {
		position: absolute;
		left: 170rpx;
		width: 75%;
		height: 87%;
		overflow-y: scroll;

		.classrmain {
			width: 100%;
			padding-bottom: 100rpx;
			flex-wrap: wrap;
			justify-content: space-around;

			.collect {
				position: absolute;
				bottom: 20rpx;
				right: 20rpx;
				z-index: 50;
			}

			.classreach {
				position: relative;
				width: 260rpx;
				height: 370rpx;
				margin-top: 20rpx;
				.classmallz{
					position:absolute;
					top: 0;
					left: 0;
					z-index:50;
					padding: 10rpx;
					line-height:48rpx;
					color: #fff;
					font-size:24rpx;
					background-color: #D31E11;
					border-top-left-radius:20rpx;
					border-bottom-right-radius:20rpx;
				}
				.classreachbg {
					position: absolute;
					z-index: 0;
					border-radius: 20rpx;
				}

				.classreachmsg {
					position: absolute;
					z-index: 10;
					left: 0;
					top: 0;
					padding: 20rpx;

					.classrgoodsimg {
						width: 216rpx;
						height: 216rpx;
					}

					.classrgoodsname {
						margin-top: 25rpx;
						line-height: 44rpx;
						color: #333333;
						font-size: 28rpx;
						font-weight: bold;
					}

					.classrgoodsmoney {
						color: #F1221B;
						font-size: 32rpx;
						margin-top: 12rpx;
					}
				}
			}
		}
	}
</style>